package com.fita.api.dto.request;

import java.math.BigInteger;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.opentravel.ota._2003._05.CancelInfoRQType;
import org.opentravel.ota._2003._05.CompanyNameType;
import org.opentravel.ota._2003._05.OTAVehCancelRQ;
import org.opentravel.ota._2003._05.POSType;
import org.opentravel.ota._2003._05.PersonNameType;
import org.opentravel.ota._2003._05.SourceType;
import org.opentravel.ota._2003._05.SourceType.RequestorID;
import org.opentravel.ota._2003._05.TransactionActionType;
import org.opentravel.ota._2003._05.UniqueIDType;
import org.opentravel.ota._2003._05.VehicleCancelRQAdditionalInfoType;

import com.fita.api.dto.common.CommonVariable;

/**
 * 
 *
 * @author LZJ
 *
 */
public class CancelExistingVehicleReservationRequest {
	
	 /**
     * 车辆可用状态
     * 
     *  - Available=可用车辆
     *  - All=所有车辆
     */	
	private String status;
	
	private String cancelType;
    
	private String uniqueID;
	
	private String uniqueType;
	
	private String givenName;
	
	private String surname;
	
	private String vendorCode;   
    
    
    private String env;
    
    

	/**
     * The default constructor
     */
    public CancelExistingVehicleReservationRequest() {
    	
    }
    
    /**
     * @param env
     * @return
     */
    public CancelExistingVehicleReservationRequest initialize(String env) {
        this.env = env;
        return this;
    }
    
    
    
    /**
     * @return
     * 
     * 校验获取的参数
     */
    public boolean isValid() {
        return StringUtils.isNotBlank(status) &&
        		uniqueID != null && 
        		uniqueType != null &&
               StringUtils.isNotBlank(givenName) && 
               StringUtils.isNotBlank(surname) &&
               vendorCode != null;
    }
    
    
    
    /**
     * @return
     * @throws ParserConfigurationException 
     */
    public OTAVehCancelRQ genecancelRq() {
        
    	OTAVehCancelRQ rq = new OTAVehCancelRQ();
        
        rq.setPrimaryLangID(CommonVariable.LANGUAGE);
        rq.setTarget(env);
        rq.setVersion(CommonVariable.VERSION);
        rq.setTransactionIdentifier("100000001");
        rq.setSequenceNmbr(new BigInteger("1"));
        
        POSType posType = this.genePos();
        rq.setPOS(posType);
 
        CancelInfoRQType core = this.genecancleRQCore();
        rq.setVehCancelRQCore(core);
        
        VehicleCancelRQAdditionalInfoType info  = this.genecancleRQInfo();
        rq.setVehCancelRQInfo(info);
        
        
        return rq;
        
    }
    
    
    
    
    /**
     * @return
     */
    private POSType genePos() {
        
        POSType pos = new POSType();
        
        // source1
        SourceType sourceType1 = new SourceType();
        sourceType1.setISOCountry("CN");
        
        // requestID
        RequestorID requestorID = new RequestorID();
        requestorID.setType(CommonVariable.REQUESTOR_TYPE);
        requestorID.setID("XMLRTA");
        
        // companyName
        CompanyNameType companyName = new CompanyNameType();
        if(StringUtils.equalsAnyIgnoreCase(env, "Production")) {
            companyName.setCode("CU");
            companyName.setCompanyShortName("CNTVLSVCXML");
        } else if(StringUtils.equalsAnyIgnoreCase(env, "Testing")) {
            companyName.setCode("EX");
            companyName.setCompanyShortName("EHIXMLTEST");
        }
        
        requestorID.setCompanyName(companyName);
        sourceType1.setRequestorID(requestorID);
        
        // source2
        SourceType sourceType2 = new SourceType();
        
        // requestID
        requestorID = new RequestorID();
        requestorID.setType(CommonVariable.REQUESTOR_TYPE);
        requestorID.setID("00000000");
        requestorID.setIDContext("IATA");
        
        sourceType2.setRequestorID(requestorID);
        
        pos.getSource().add(sourceType1);
        pos.getSource().add(sourceType2);
        
        return pos;
        
    }
    
    
    private CancelInfoRQType genecancleRQCore() {
    	
    	CancelInfoRQType core = new CancelInfoRQType();
    	
    	
    	if(this.cancelType == "Book") {
    		
    		core.setCancelType(TransactionActionType.BOOK);
    		
    	}else if (this.cancelType == "Quote") {
    		
    		core.setCancelType(TransactionActionType.QUOTE);  		
    		
    	}else if (this.cancelType == "Hold") {
    		
    		core.setCancelType(TransactionActionType.HOLD); 
    		
    	}else if (this.cancelType == "Initiate") {
    		
    		core.setCancelType(TransactionActionType.INITIATE); 
    		
    	}else if (this.cancelType == "Ignore") {
    		
    		core.setCancelType(TransactionActionType.IGNORE); 
    		
    	}else if (this.cancelType == "Modify") {
    		
    		core.setCancelType(TransactionActionType.MODIFY);
    		
    	}else if (this.cancelType == "Commit") {
    		
    		core.setCancelType(TransactionActionType.COMMIT);
    		
    	}else if (this.cancelType == "Cancel") {
    		
    		core.setCancelType(TransactionActionType.CANCEL);
    		
    	}else if (this.cancelType == "CommitOverrideEdits") {
    		
    		core.setCancelType(TransactionActionType.COMMIT_OVERRIDE_EDITS); 
    		
    	}else if (this.cancelType == "VerifyPrice") {
    		
    		core.setCancelType(TransactionActionType.VERIFY_PRICE); 
    		
    	}else if (this.cancelType == "Ticket") {
    		
    		core.setCancelType(TransactionActionType.TICKET); 
    		
    	}
    	
    	UniqueIDType unique = new UniqueIDType();
    	unique.setID(this.uniqueID);
    	unique.setType(this.uniqueType);
    	
    	core.getUniqueID().add(unique);
    	
    	
    	PersonNameType person = new PersonNameType();
    	person.getGivenName().add(this.givenName);
    	person.setSurname(this.surname);
    	
    	core.setPersonName(person);  	
    	
    	return core;
    	
    }
    
    
    private VehicleCancelRQAdditionalInfoType genecancleRQInfo() {
    	
    	VehicleCancelRQAdditionalInfoType info = new VehicleCancelRQAdditionalInfoType();
    	
    	CompanyNameType vendor = new CompanyNameType();
    	
    	vendor.setCode(this.vendorCode);
    	
    	info.setVendor(vendor);  	
    	
    	
    	return info;
    }
    
    
  

	public String getEnv() {
		return env;
	}

	public void setEnv(String env) {
		this.env = env;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getUniqueID() {
		return uniqueID;
	}

	public void setUniqueID(String uniqueID) {
		this.uniqueID = uniqueID;
	}

	public String getUniqueType() {
		return uniqueType;
	}

	public void setUniqueType(String uniqueType) {
		this.uniqueType = uniqueType;
	}

	public String getGivenName() {
		return givenName;
	}

	public void setGivenName(String givenName) {
		this.givenName = givenName;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getVendorCode() {
		return vendorCode;
	}

	public void setVendorCode(String vendorCode) {
		this.vendorCode = vendorCode;
	}

	
    
}
